package cn.agiledata.bank.webService;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.webService.constant.IVRConstant;
import cn.agiledata.bank.webService.faced.IIVRWebServiceFaced;
import cn.agiledata.bank.webService.faced.IVRWebServiceFaced;
import cn.agiledata.bank.webService.formbean.IVRRequest;
import cn.agiledata.bank.webService.formbean.IVRResponse;

import org.apache.log4j.Logger;

public class MainService {
	private static Logger logger = Logger
			.getLogger(cn.agiledata.bank.webService.MainService.class);

	private MainServiceTemplement templement = new MainServiceTemplement();

	public MainService() {
		super();
	}

	/**
	 * 用户验证
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse loginUser(IVRRequest request) {

		IVRResponse response = new IVRResponse();
		/** check the request whether it is null */
		//if (!WebServiceUtil.checkUser(request, response)) {
		if (!checkUserLoginpasswd(request, response, "loginUser")) {
			logger.info("not find the request");
			return response;
		}
		//response = this.getIVRFaced().loginUser(request);
		response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		return response;
	}


	
	/**
	 * 得到银行卡列表接口
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse accountList(IVRRequest request) {
		IVRResponse response = new IVRResponse();

		/** check the user whether it is null */
		
		if (!checkUserLoginpasswd(request, response, "accountList")) {
			logger
					.info("The accountList method ---> not find response or not find login user ");
			return response;
		}
		
		response = this.getIVRFaced().accountList(request);
		return response;
		/*
		// TODO business logic interface
		response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		return response;
		*/
	}

	/** wingfeng add this method on depend the current doc 2006-11-24 */
	/**
	 * 账户提现
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse accountDistillCash(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		//request.setSubaccountid("1143");
		//request.setPoundage(new Double(22.2));
		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response, "accountDistillCash")) {
			logger
					.info("The accountDistillCash method ---> not find response or not find login user ");

			return response;
		}
		response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		response = getIVRFaced().accountDistillCash(request);
		return response;
	}

	/**
	 * 网上付款
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse accountTransfer(IVRRequest request) {
		IVRResponse response = new IVRResponse();

		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response, "accountTransfer")) {
			logger
					.info("The accountTransfer method ---> not find response or not find login user ");

			return response;
		}
		response = getIVRFaced().accountTransfer(request);
		return response;
	}



	/**
	 * 取得子账户列表
	 * 
	 * @param request
	 * @return ????????????????????????
	 */
	public IVRResponse getSubAccountList(IVRRequest request) {
		IVRResponse response = new IVRResponse();

		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response, "getSubAccountList")) {
			logger
					.info("The getSubAccountList method ---> not find response or not find login user ");
			// response.setResult(IVRConstant.COMMON_USER_NOT_FIND);
			return response;
		}

		response = getIVRFaced().getSubAccountList(request);
		return response;

	}

	/**
	 * 查询子账户余额
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse searchSubaccountBlance(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		// request.setSubaccountid("ddddd");
		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response, "searchSubaccountBlance")) {
			logger
					.info("The searchSubaccountBlance method ---> not find response or not find login user ");
			return response;
		}
		response = this.getIVRFaced().searchSubaccountBlance(request);
		return response;
	}

	/**
	 * 查询积分
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse searchIntegral(IVRRequest request) {
		IVRResponse response = new IVRResponse();

		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response, "searchIntegral")) {
			logger
					.info("The searchIntegral method ---> not find response or not find login user ");
			return response;
		}
		response = this.getIVRFaced().searchIntegral(request);
		return response;
	}

	/**
	 * 查询交易历史
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse searchTransactionHistory(IVRRequest request) {
		IVRResponse response = new IVRResponse();

		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response, "searchTransactionHistory")) {
			logger
					.info("The searchTransactionHistory method ---> not find response or not find login user ");

			return response;
		}

		response = this.getIVRFaced().searchTransactionHistory(request);

		return response;
	}

	/**
	 * 待处理交易
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse searchWaitTransaction(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		/*
		 * request.setSubaccountid("1231231"); request.setSearchdate("07-01");
		 */
		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response, "searchWaitTransaction")) {
			logger
					.info("The searchWaitTransaction method ---> not find response or not find login user ");

			return response;
		}

		response = getIVRFaced().searchWaitTransaction(request);
		return response;

	}
	
	/**
	 * 修改电话登录密码
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse modifyPhoneLoginPasswd(IVRRequest request) {
		IVRResponse response = new IVRResponse();

		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response, "modifyPhoneLoginPasswd")) {
			logger
					.info("The modifyPhoneLoginPasswd method ---> not find response or not find login user ");

			return response;
		}
		response = getIVRFaced().modifyPhoneLoginPasswd(request);
		return response;
	}

	/**
	 * 修改电话支付密码
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse modifyPhoneLoginPaymentPasswd(IVRRequest request) {
		IVRResponse response = new IVRResponse();

		/** check the user whether it is null */
		if (!checkUserLoginpasswd(request, response,
				"modifyPhoneLoginPaymentPasswd")) {
			logger
					.info("The modifyPhoneLoginPaymentPasswd method ---> not find response or not find login user ");

			return response;
		}
		response = getIVRFaced().modifyPhoneLoginPaymentPasswd(request);
		return response;
	}



	/**
	 * 用户预注册接口
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse PrepRegister(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		// check user
		if (null == request) {
			response.setResult(IVRConstant.COMMON_PARAMTER_DEFORMITY);
			return response;
		}

		// if (null == request.getAccounttype())
		request.setAccounttype(Constant.ACCOUNT_TYPE_BUYER);

		getIVRFaced().PrepRegister(request);
		return response;
	}

	/**
	 * 交易手续费接口
	 * 
	 * @param request
	 * @return
	 */
	public IVRResponse getTransactionPoundage(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		// check user

		if (null == request.getMoneytype())
			request.setMoneytype("rmb");
		if (!WebServiceUtil.checkUser(request, response,
				"getTransactionPoundage")) {
			logger
					.info(" MainService --> PrepRegister(IVRRequest resquest)  info: IVR user is not find!");
			return response;
		}
		response = this.getIVRFaced().getTransactionPoundage(request);
		return response;
	}

	// /////////////////// some help method ///////////////////////////////

	/**
	 * check the payment passwd.
	 * 
	 * @param request
	 * @return check result false -- no find user true -- find user.
	 */
	private boolean checkUserLoginpasswd(IVRRequest request,
			IVRResponse response, String methodName) {

		String checkFields;

		/** check the request whether it is null */
		if (!WebServiceUtil.checkUser(request, response, methodName)) {
			return false;
		}

		checkFields = WebServiceUtil.getCheckFields4MethodName(methodName
				.toLowerCase());
		if (!WebServiceUtil.checkRequest(request, response, checkFields)) {
			return false;
		}
		IVRResponse responseUser = this.getIVRFaced().loginUser(request);
		if (!IVRConstant.COMMON_RESULT_TRUE.equals(responseUser.getResult())) {
			response.setResult(responseUser.getResult());
			return false;
		}

		// TODO check the user whether it is not null
		return true;

	}

//	/**
//	 * check the payment passwd.
//	 * 
//	 * @param request
//	 * @return check result false -- no find user true -- find user.
//	 */
//	private boolean checkUserPaymentpasswd(IVRRequest request,
//			IVRResponse response, String methodName) {
//		/** check the request whether it is null */
//		if (!WebServiceUtil.checkUser(request, response, methodName)) {
//			return false;
//		}
//
//		IVRResponse responseUser = this.getIVRFaced().loginUser(request);
//		if (!IVRConstant.COMMON_RESULT_TRUE.equals(responseUser.getResult())) {
//			response.setResult(responseUser.getResult());
//			return false;
//		}
//		response.setResult(responseUser.getResult());
//		return true;
//	}

	/**
	 * Get the IVR service bean instance.
	 * 
	 * @param beanName
	 *            the serive bean name.
	 * @return the serive bean instance.
	 */
	private Object getBean(String beanName) {
		Object obj = IVRServiceBeanFactory.getIVRServiceBeanInstrance(beanName);
		return obj;
	}

	/**
	 * Get IVRFaced instance.
	 * 
	 * @return
	 */
	private IIVRWebServiceFaced getIVRFaced() {
		return new IVRWebServiceFaced();
	}
	
//	/**
//	 * 订单支付
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse orderFormPayment(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		if (null == request.getMoneytype())
//			request.setMoneytype(Constant.CURRENCY_RMB);
//		/** check the user whether it is null */
//		if (!checkUserPaymentpasswd(request, response, "orderFormPayment")) {
//			logger
//					.info("The accountTopup method ---> not find response or not find login user ");
//			return response;
//		}
//
//		// TODO business logic interface
//		// response.setResult(IVRConstant.COMMON_RESULT_TRUE);
//		response = this.getIVRFaced().orderFormPayment(request);
//		return response;
//	}

//	/**
//	 * 账户充值
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse accountTopup(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check the user whether it is null */
//		if (!checkUserPaymentpasswd(request, response, "accountTopup")) {
//			logger
//					.info("The accountTopup method ---> not find response or not find login user ");
//			return response;
//		}
//		response = this.getIVRFaced().accountTopup(request);
//		return response;
//		/*
//		// TODO business logic interface
//		response.setResult(IVRConstant.COMMON_RESULT_TRUE);
//		return response;
//		*/
//	}
	

//	/**
//	 * 账户付款
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse accountPayment(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check the user whether it is null */
//		if (!checkUserPaymentpasswd(request, response, "accountPayment")) {
//			logger
//					.info("The accountPayment method ---> not find response or not find login user ");
//
//			return response;
//		}
//		response = getIVRFaced().accountPayment(request);
//		return response;
//	}
	

//	/**
//	 * 银行卡互转
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse bankCardChange(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check the user whether it is null */
//		if (!checkUserPaymentpasswd(request, response, "bankCardChange")) {
//			logger
//					.info("The bankCardChange method ---> not find response or not find login user ");
//
//			return response;
//		}
//		response = getIVRFaced().bankCardChange(request);
//		return response;
//	}
	


//	/**
//	 * 查询用户
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse searchAccount(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check the user whether it is null */
//		if (!checkUserLoginpasswd(request, response, "searchAccount")) {
//			logger
//					.info("The searchAccount method ---> not find response or not find login user ");
//
//			return response;
//		}
//
//		if (!("1".equals(request.getUserId()))) {
//			response.setResult(IVRConstant.COMMON_USER_NOT_FIND);
//			return response;
//		}
//
//		response = getIVRFaced().searchAccount(request);
//		return response;
//	}
//	/**
//	 * 设置电话支付密码
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse setPhoneLoginPaymentPasswd(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//		/** check the user whether it is null */
//		if (!checkUserLoginpasswd(request, response,
//				"setPhoneLoginPaymentPasswd")) {
//			logger
//					.info("The setPhoneLoginPaymentPasswd method ---> not find response or not find login user ");
//
//			return response;
//		}
//		response = getIVRFaced().setPhoneLoginPaymentPasswd(request);
//		return response;
//	}
//
//	/**
//	 * 修改交易最大限额
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse modifyTransactionBiglimit(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check the user whether it is null */
//		if (!checkUserLoginpasswd(request, response,
//				"modifyTransactionBiglimit")) {
//			logger
//					.info("The modifyTransactionBiglimit method ---> not find response or not find login user ");
//
//			return response;
//		}
//		response = getIVRFaced().modifyTransactionBiglimit(request);
//		return response;
//	}
//	/**
//	 * 取得用户子账户
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse getSubAccount(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check the user whether it is null */
//		if (!checkUserLoginpasswd(request, response, "getSubAccount")) {
//			logger
//					.info("The getSubAccount method ---> not find response or not find login user ");
//
//			return response;
//		}
//		response = this.getIVRFaced().getSubAccount(request);
//		return response;
//	}
//	
//	/**
//	 * 取得用户子账户
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse getSubAccountBalance(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check the user whether it is null */
//		if (!checkUserLoginpasswd(request, response, "getSubAccountList")) {
//			logger
//					.info("The getSubAccountList method ---> not find response or not find login user ");
//			// response.setResult(IVRConstant.COMMON_USER_NOT_FIND);
//			return response;
//		}
//
//		//response = getIVRFaced().getSubAccountBalance(request);
//		response = getIVRFaced().searchSubaccountBlance(request);
//		return response;
//	}
//
//	/**
//	 * 取得用户交易历史
//	 * 
//	 * @param request
//	 * @return
//	 */
//	// public IVRResponse getTransactionHistory(IVRRequest request) {
//	// IVRResponse response = new IVRResponse();
//	//
//	// /** check the user whether it is null */
//	// if (!checkUserLoginpasswd(request, response,"getTransactionHIstory")) {
//	// logger.info(
//	// "The getTransactionHistroy method ---> not find response or not find
//	// login user ");
//	//
//	// return response;
//	// }
//	// response = this.getIVRFaced().getTransactionHistory(request);
//	// return response;
//	// }
//	/**
//	 * 取得支持的银行
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse getSupportBank(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check user */
//		if (!WebServiceUtil.checkUser(request, response, "getSupportBank")) {
//			logger
//					.info(" MainService --> getBlankList(IVRRequest resquest)  info: IVR user is not find!");
//			return response;
//		}
//
//		response = this.getIVRFaced().getSupportBank(request);
//		response.setResult(IVRConstant.COMMON_RESULT_TRUE);
//		/** set the mock bank array */
//
//		return response;
//	}
//
//	/**
//	 * 取得用户账户
//	 * 
//	 * @param request
//	 * @return
//	 */
//	public IVRResponse getAccount(IVRRequest request) {
//		IVRResponse response = new IVRResponse();
//
//		/** check the user whether it is null */
//		if (!checkUserLoginpasswd(request, response, "getAccount")) {
//			logger
//					.info("The getAccount method ---> not find response or not find login user ");
//
//			return response;
//		}
//
//		response = this.getIVRFaced().getAccount(request);
//
//		return response;
//	}


}
